let layer, element

$(function () {
    layui.use(['table', 'form', 'laydate', 'element', 'layer',], function () {
        layer = layui.layer, element = layui.element
    })
    // 鼠标悬停个人中心头像
    $('.login-user-icon').hover(function () {
        $('.user-login-panel').show();
    }, function () {
        $('.user-login-panel').hide(20);
    })
    // 获取验证码按钮状态
    $(document).on('blur', '#login-email', function () {
    })
})


/**
 * 获取手机验证码
 */
function getVerification() {
    let stuMobile = $.trim($('#register-stuMobile').val())
    if (stuMobile === null || stuMobile === '') {
        layer.tips('注册手机不能为空', '#register-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (!reg.test(stuMobile)) {
        layer.tips('注册手机格式不正确', '#register-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    let student = {stuMobile: stuMobile}
    post(api.GetVerification, student).then(res => {
        $('.phone-code-box a').attr("disabled", true); //设置成灰色不可点击
        $('.phone-code-box a').css("pointer-events", "none");  //设置鼠标事件不可用
        $('.phone-code-box a').addClass('a-not-click')
        let time = 30
        $('.phone-code-box a').text(time + '秒后可用')
        let id = setInterval(function () {
            time--
            if (time <= 1) {
                $('.phone-code-box a').text('获取验证码')
                $('.phone-code-box a').attr("disabled", false);
                $('.phone-code-box a').css("pointer-events", "auto");
                $('.phone-code-box a').removeClass('a-not-click')
                clearInterval(id)
                return
            }
            $('.phone-code-box a').text(time + '秒后可用')
        }, 1000)
    })
}

/**
 * 获取邮箱验证码
 * @param layer
 */
function loadVerification() {
    let email = $.trim($('#register-email').val())
    // 验证邮箱格式
    let emialReg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
    if (email === null || email === undefined || email === '') {
        layer.tips('登录邮箱格式不正确', '#register-email', {tips: [3, '#202d4a']});
        return false
    }
    if (!emialReg.test(email)) {
        layer.tips('登录邮箱格式不正确', '#register-email', {tips: [3, '#202d4a']});
        return false
    }
    layer.msg('发送中，请稍后...', {icon: 16, shade: 0.5});
    let stu = {stuEmail: email}
    post(api.GetEmailVerification, stu).then(res => {
        if (res.code === '000000') {
            layer.msg('验证码发送成功...', {icon: 1, time: 1000});
        } else if (res.code === '000004') {
            layer.tips('该邮箱已存在，请更换邮箱重新注册', '#register-email', {tips: [3, '#202d4a']});
        }
    })
}


/**
 * 注册时获取邮箱验证码
 */
function emailRegister() {
    let email = $.trim($('#register-email').val())
    let emailCode = $.trim($('#register-email-code').val())
    let password = $.trim($('#register-email-password').val())
    let again_password = $.trim($('#again-register-email-password').val())
    let name = $.trim($('#register-email-name').val())
    if (email === null || email === undefined || email === '') {
        layer.tips('注册邮箱不能为空', '#register-email', {tips: [3, '#202d4a']});
        return false
    }
    let email_reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
    if (!email_reg.test(email)) {
        layer.tips('注册邮箱格式不正确', '#register-email', {tips: [3, '#202d4a']});
        return false
    }
    if (emailCode === null || emailCode === undefined || emailCode === '') {
        layer.tips('邮箱验证码不能为空', '#register-email-code', {tips: [3, '#202d4a']});
        return false
    }
    if (password === null || password === '') {
        layer.tips('登录密码不能为空', '#register-email-password', {tips: [3, '#202d4a']});
        return false
    }
    if (again_password !== password) {
        layer.tips('两次密码不一致', '#again-register-email-password', {tips: [3, '#202d4a']});
        return false
    }
    if (name === null || name === '') {
        layer.tips('姓名不能为空', '#register-email-name', {tips: [3, '#202d4a']});
        return false
    }
    let student = {stuEmail: email, code: emailCode, stuName: name, password: password};
    post(api.SaveEmailStudent, student).then(res => {
        if (res.code === '000000') {
            location.href = '/'
        } else if (res.code === '000004') {
            layer.tips('邮箱重复重复', '#register-stuMobile', {tips: [3, '#202d4a']});
        } else if (res.code === '000301') {
            layer.msg('验证码异常', {icon: 5});
        }
    })
}

/**
 * 注册用户信息
 */
function register() {
    let stuMobile = $.trim($('#register-stuMobile').val())
    let code = $.trim($('#register-code').val())
    let password = $.trim($('#register-password').val())
    let again_password = $.trim($('#again-register-password').val())
    let name = $.trim($('#register-name').val())
    if (stuMobile === null || stuMobile === '') {
        layer.tips('登录手机不能为空', '#register-stuMobile', {tips: [3, '#202d4a']});
        return false
    }
    if (code === null || code === '') {
        layer.tips('手机验证码不能为空', '#register-code', {tips: [3, '#202d4a']});
        return false
    }
    if (password === null || password === '') {
        layer.tips('登录密码不能为空', '#register-password', {tips: [3, '#202d4a']});
        return false
    }
    if (again_password !== password) {
        layer.tips('两次密码不一致', '#register-password', {tips: [3, '#202d4a']});
        return false
    }
    if (name === null || name === '') {
        layer.tips('姓名不能为空', '#register-name', {tips: [3, '#202d4a']});
        return false
    }
    // let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    // if (recommendPhone !== null && recommendPhone !== '') {
    //     if (!reg.test(recommendPhone)) {
    //         layer.tips('推荐人手机号码格式不正确', '#register-recommend-phone', {tips: [3, '#202d4a']});
    //         return false
    //     }
    // }
    let studentEntity = {
        stuMobile: stuMobile,
        stuName: name,
        code: code,
        password: password,
        //recommendPhone: recommendPhone
    }
    post(api.SaveStudent, studentEntity).then(res => {
        if (res.code === '000000') {
            location.href = '/'
        } else if (res.code === '000008') {
            layer.tips('该手机已注册，去登录', '#register-stuMobile', {tips: [3, '#202d4a']});
        } else if (res.code === '000301') {
            layer.msg('验证码异常', {icon: 5});
        }
    })
}
